package com.atguigu.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class test_es_10_doc_search {

    public static void main(String[] args) throws IOException {
        test3();
    }

    public static void test3() throws IOException {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));

        //创建查询对象
        SearchRequest searchRequest = new SearchRequest();
        //创建查询体
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //组合查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gte(20).lte(30));
//        boolQueryBuilder.must(QueryBuilders.fuzzyQuery("name","zhangsan").fuzziness(Fuzziness.TWO));;

        searchSourceBuilder.aggregation(AggregationBuilders.min("minAge").field("age"));

        searchSourceBuilder.query(boolQueryBuilder);

        searchRequest.source(searchSourceBuilder);

        //查询、发送请求
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println(searchResponse);

        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        System.out.println(searchResponse.getAggregations().getAsMap());

        //关闭es客户端
        esClient.close();


    }

    public static void test2() throws IOException {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));

        //创建查询请求对象
        SearchRequest searchRequest = new SearchRequest("user");

        //创建查询请求体对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery()); //设置查询类型
        //分页
//        searchSourceBuilder.from(0);
//        searchSourceBuilder.size(2);

        //排序
//        searchSourceBuilder.sort("age", SortOrder.ASC);
//        searchSourceBuilder.sort("name",SortOrder.ASC);

        //过滤字段
//        String [] includes = {"name"};
//        String []  excludes = {};
//        searchSourceBuilder.fetchSource(includes,excludes);

        searchRequest.source(searchSourceBuilder);

        //查询、发送请求
        SearchResponse searchResponse = esClient.search(searchRequest,RequestOptions.DEFAULT);


        SearchHits hits = searchResponse.getHits();

        System.out.println("took: "+searchResponse.getTook());
        System.out.println("timeout: "+ searchResponse.isTimedOut());
        System.out.println("total: "+hits.getTotalHits());
        System.out.println("MaxScore: "+hits.getMaxScore());
        System.out.println("==================>");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        //关闭es客户端
        esClient.close();
    }

    public static void test1() throws IOException {

        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));

        //创建查询请求对象
        SearchRequest searchRequest = new SearchRequest("user");
        //创建查询源对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        searchRequest.source(searchSourceBuilder);

        SearchResponse  searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);

        //查询匹配
        System.out.println(searchResponse);
        SearchHits hits = searchResponse.getHits();

        System.out.println("took: "+ searchResponse.getTook());
        System.out.println("timeout: "+searchResponse.isTimedOut());
        System.out.println("total: "+ hits.getTotalHits());
        System.out.println("maxScore: "+hits.getMaxScore());
        System.out.println("======================>");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        //关闭es客户端
        esClient.close();
    }
}
